package com.tddx.activiti.utils.gentiJava;

import java.util.List;

import com.tddx.activiti.utils.StringUtils;

public class GenContent {
	
	private static String querySql = "";

	public static String getEntityContent() {
		List<TablePro> list = Constants.tableProlist;
		StringBuffer sb = new StringBuffer("package " + Constants.ENTITYPACKAGE + ";\r\n\r\n");
		sb.append("import javax.persistence.*;\r\n\r\n");
		sb.append("import java.io.Serializable;\r\n\r\n");
		sb.append("import java.util.Date;\r\n\r\n");
		sb.append("import org.springframework.format.annotation.DateTimeFormat;\r\n\r\n");
		sb.append(getNote("entity"));
		sb.append("@Entity\r\n");
		sb.append("@Table(name=\"" + Constants.TABLENAME + "\")\r\n");
		sb.append("public class " + Constants.ENTITYJAVANAME + " implements Serializable{\r\n\r\n");
		for(TablePro tp: list){
			String colName = tp.getColName();
			String proName = getProName(colName);
			if("1".equals(tp.getIsPrimary())){
				sb.append("	@Id\r\n");
				sb.append("	@GeneratedValue(strategy = GenerationType.AUTO)\r\n");
			}
			if(DataType.NUMBER.toString().equals(tp.getDataType())){
				sb.append("	@Column(name = \"" + colName + "\")\r\n");
				sb.append("	private Integer " + proName + ";\r\n\r\n");
			}else if(DataType.DATE.toString().equals(tp.getDataType())){
				sb.append("	@Column(name = \"" + colName + "\")\r\n");
				sb.append("	@DateTimeFormat(pattern=\"yyyy-MM-dd HH:mm:ss\")\r\n");
				sb.append("	private Date " + proName + ";\r\n\r\n");
			}else if(DataType.LONG.toString().equals(tp.getDataType())){
				sb.append("	@Column(name = \"" + colName + "\")\r\n");
				sb.append("	private Long " + proName + ";\r\n\r\n");
			}else{
				sb.append("	@Column(name = \"" + colName + "\")\r\n");
				sb.append("	private String " + proName + ";\r\n\r\n");
			}
			querySql += colName + " " + proName + ", ";
		}
		if(querySql.endsWith(", ")){
			querySql = querySql.substring(0, querySql.length()-2);
		}
		sb.append("}");
		return sb.toString();
	}
	
	public static String getJdbcContent() {
		StringBuffer sb = new StringBuffer("package " + Constants.REPOSITORYPACKAGE + ";\r\n\r\n");
		sb.append("import java.util.List;\r\n\r\n");
		sb.append("import org.springframework.data.jpa.repository.JpaRepository;\r\n");
		sb.append("import org.springframework.stereotype.Repository;\r\n\r\n");
		sb.append("import org.springframework.jdbc.core.JdbcTemplate;\r\n\r\n");
		sb.append("import org.springframework.jdbc.core.BeanPropertyRowMapper;\r\n\r\n");
		sb.append("import org.springframework.beans.factory.annotation.Autowired;\r\n\r\n");
		sb.append("import com.tiedate.csmis.system.login.User;\r\n\r\n");
		sb.append("import " + Constants.ENTITYPACKAGE + "." + Constants.ENTITYJAVANAME + ";\r\n\r\n");
		
		sb.append(getNote("repository"));
		sb.append("@Repository\r\n");
		sb.append("public class " + Constants.JDBCJAVANAME +  "{\r\n\r\n");
		sb.append("	@Autowired private JdbcTemplate jdbc;\r\n\r\n");
		sb.append("	public Object findList(" + Constants.ENTITYJAVANAME + " entity, User user){\r\n\r\n");
		sb.append("		String sql=\"SELECT " + querySql + "\"\r\n");
		sb.append("			+ \" FROM " + Constants.TABLENAME + "\";\r\n\r\n");
		sb.append("		List<" + Constants.ENTITYJAVANAME + "> list = jdbc.query(sql,BeanPropertyRowMapper.newInstance(" + Constants.ENTITYJAVANAME + ".class));\r\n");
		sb.append("		return list;\r\n");
		sb.append("	}\r\n");
		sb.append("}\r\n");
		return sb.toString();
	}
	
	public static String getDaoContent() {
		StringBuffer sb = new StringBuffer("package " + Constants.REPOSITORYPACKAGE + ";\r\n\r\n");
		sb.append("import java.util.List;\r\n\r\n");
		sb.append("import org.springframework.data.jpa.repository.JpaRepository;\r\n");
		sb.append("import org.springframework.stereotype.Repository;\r\n\r\n");
		sb.append("import " + Constants.ENTITYPACKAGE + "." + Constants.ENTITYJAVANAME + ";\r\n\r\n");

		sb.append(getNote("repository"));
		sb.append("@Repository\r\n");
		sb.append("public interface " + Constants.REPOSITORYJAVANAME + 
				" extends JpaRepository<" + Constants.ENTITYJAVANAME + ", String>{\r\n\r\n");
		sb.append("}\r\n");
		return sb.toString();
	}

	
	public static String getServiceContent() {
		StringBuffer sb = new StringBuffer("package " + Constants.SERVICEPACKAGE + ";\r\n\r\n");
		sb.append("import java.util.List;\r\n\r\n");
		sb.append("import java.util.ArrayList;\r\n\r\n");
		sb.append("import org.springframework.beans.factory.annotation.Autowired;\r\n");
		sb.append("import org.springframework.stereotype.Service;\r\n\r\n");
		sb.append("import " + Constants.ENTITYPACKAGE + "." + Constants.ENTITYJAVANAME + ";\r\n");
		sb.append("import " + Constants.REPOSITORYPACKAGE + "." + Constants.REPOSITORYJAVANAME + ";\r\n\r\n");

		sb.append(getNote("service"));
		sb.append("@Service\r\n");
		sb.append("public class " + Constants.SERVICEJAVANAME + " {\r\n\r\n");
		sb.append("	@Autowired private " + Constants.REPOSITORYJAVANAME + " dao;\r\n\r\n");
		sb.append("	@Autowired private " + Constants.JDBCJAVANAME + " jdbc;\r\n\r\n");
		
		//开始写方法
		//查询实体类
		sb.append("	public " + Constants.ENTITYJAVANAME + " findOne(String id){\r\n");
		sb.append("		" + Constants.ENTITYJAVANAME + " entity = dao.findOne(id);\r\n");
		sb.append("		return entity;\r\n");
		sb.append("	}\r\n");
		
		//保存实体类
		sb.append("	@Transactional(\"transactionManagerJpa\")\r\n");
		sb.append("	public " + Constants.ENTITYJAVANAME + " save(" + Constants.ENTITYJAVANAME + " entity){\r\n");
		sb.append("		entity = dao.saveAndFlush(entity);\r\n");
		sb.append("		return entity;\r\n");
		sb.append("	}\r\n");
		
		//删除实体类
		sb.append("	public " + Constants.ENTITYJAVANAME + " delete(" + Constants.ENTITYJAVANAME + " entity){\r\n");
		sb.append("		entity = dao.findOne(entity.getId());\r\n");
		sb.append("		entity.setVcDel(\"1\");\r\n");
		sb.append("		return entity;\r\n");
		sb.append("	}\r\n");
		
		//实体类
		sb.append("	public Object findList(" + Constants.ENTITYJAVANAME + " entity){\r\n");
		sb.append("		Object obj = jdbc.findList(entity);\r\n");
		sb.append("		return obj;\r\n");
		sb.append("	}\r\n");
		
		sb.append("}\r\n");
		return sb.toString();
	}
	
	public static String getCtrlContent() {
		StringBuffer sb = new StringBuffer("package " + Constants.CONTROLLERPACKAGE + ";\r\n\r\n");
		
		sb.append("import java.util.Date;\r\n\r\n");
		sb.append("import org.springframework.beans.factory.annotation.Autowired;\r\n");
		sb.append("import org.springframework.stereotype.Controller;\r\n\r\n");
		sb.append("import org.springframework.web.bind.annotation.RequestMapping;\r\n\r\n");
		sb.append("import " + Constants.ENTITYPACKAGE + "." + Constants.ENTITYJAVANAME + ";\r\n");
		sb.append("import " + Constants.SERVICEPACKAGE + "." + Constants.SERVICEJAVANAME + ";\r\n\r\n");
		
		sb.append(getNote("ctrl"));
		sb.append("@Controller\r\n");
		String topMap = Constants.TOPPACKNAME.substring(Constants.TOPPACKNAME.lastIndexOf(".") + 1);//截取最后一个文件夹的名称如：com.lei.demo.system截取system
		sb.append("@RequestMapping(\"" + topMap + "/" + Constants.PACKAGENAME + "\")\r\n");
		sb.append("public class " + Constants.CONTROLLERJAVANAME + " {\r\n\r\n");
		sb.append("	@Autowired private " + Constants.SERVICEJAVANAME + " service;\r\n\r\n");
		
		//开始写方法；
		//查看页面的方法
		sb.append("	@RequestMapping(\"view{oper}\")\r\n");
		sb.append("	public String view(@PathVariable String oper, Long id, ModelMap model){\r\n");
		sb.append("		if(\"result\".equals(oper)){\r\n");
		sb.append("			return \"modules/sys/sysTuserList\";");
		sb.append("		}");
		sb.append("		if(\"result\".equals(oper)){\r\n");
		sb.append("			model.addAttribute(\"entity\", service.findOne(id));\r\n");
		sb.append("		}");
		sb.append("		model.addAttribute(\"oper\", oper);\r\n");
		sb.append("		return \"" + Constants.PACKAGENAME + "\" + StringUtils.capitalize(oper) + \".jsp\";\r\n");
		sb.append("	}\r\n");
		
		//保存实体类
		sb.append("	@RequestMapping(\"save\")\r\n");
		sb.append("	@ResponseBody\r\n");
		sb.append("	public " + Constants.ENTITYJAVANAME + " save(" + Constants.ENTITYJAVANAME  + " entity){\r\n");
		sb.append("		entity = service.save(entity);\r\n");
		sb.append("		return entity;\r\n");
		sb.append("	}\r\n");
		
		//删除实体类
		sb.append("	@RequestMapping(\"delete\")\r\n");
		sb.append("	@ResponseBody\r\n");
		sb.append("	public Object delete(String ids){\r\n");
		sb.append("		service.delete(ids);\r\n");
		sb.append("		return ids;\r\n");
		sb.append("	}\r\n");
		
		//实体类
		sb.append("	@RequestMapping({\"list\", \"\"})\r\n");
		sb.append("	@ResponseBody\r\n");
		sb.append("	public Object findList(" + Constants.ENTITYJAVANAME + " entity){\r\n");
		sb.append("		Object obj = service.findList(entity);\r\n");
		sb.append("		return obj;\r\n");
		sb.append("	}\r\n");
		sb.append("}\r\n");
		return sb.toString();
	}

	public static String getResultContent(String name){
		StringBuffer sb = new StringBuffer();
		sb.append("<%@page language=\"java\" contentType=\"text/html;charset=UTF-8\"%>\r\n");
		sb.append("<!DOCTYPE html>\r\n<html>\r\n<head>\r\n");
		sb.append("	<title>" + name + "列表</title>\r\n");
		sb.append("	<meta charset=\"utf-8\">");
		sb.append(" <style>\r\n		.table {\r\n			padding:10px;\r\n		}\r\n	</style>\r\n");
		sb.append("	<%@include file=\"/common/table.jsp\"%>\r\n");
		sb.append("</head>\r\n");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		return sb.toString();
	}

	public static String getInputContent(String name){
		StringBuffer sb = new StringBuffer();
		sb.append("<%@page language=\"java\" contentType=\"text/html;charset=UTF-8\"%>\r\n");
		sb.append("<!DOCTYPE html>\r\n<html>\r\n<head>\r\n");
		sb.append("	<title>" + name + "列表</title>\r\n");
		sb.append("	<meta charset=\"utf-8\">");
		sb.append(" <style>\r\n		.table {\r\n			padding:10px;\r\n		}\r\n	</style>\r\n");
		sb.append("	<%@include file=\"/common/table.jsp\"%>\r\n");
		sb.append("</head>\r\n");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		return sb.toString();
	}
	
	public static String printColumString(Object col) {
		String columString = "@Column(name=\"" + col + "\")\r\n"
				+ "private String "
				+ StringUtils.lowerCase(String.valueOf(col) + ";\n");
		return columString;
	}
	
	/**
	 * 格式化column name，用以符合属性的命名
	 * @param str
	 * @return
	 */
	public static String getProName(String str){
		str = StringUtils.lowerCase(str);
		return StringUtils.lowerFirst(str, "_","\\$");
	}
	
	/**
	 * 获得注释
	 * @return
	 */
	public static String getNote(String identy){
		StringBuffer sb = new StringBuffer();
		sb.append("/**\r\n");
		if("ctrl".equals(identy)){
			sb.append(" * control layer 控制层 \r\n");
		}else if("service".equals(identy)){
			sb.append(" * service layer 服务层 \r\n");
		}else if("repository".equals(identy)){
			sb.append(" * repository layer 数据层 \r\n");
		}else{
			sb.append(" * 实体类 \r\n");
		}
		sb.append(" * @Auth yanweizhao \r\n");
		sb.append("*/\r\n");
		return sb.toString();
	}
	
	/**
	 * 获得ctrl公用的
	 * User， Date转换
	 */
	@SuppressWarnings("unused")
	private static String getCtrlMethodString(){
		
		StringBuffer sb = new StringBuffer("private User user;\r\n\r\n");
		return sb.toString();
		
	}
	
}
